package com.qk.controller;

import com.qk.anno.Log;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.CourseDto;
import com.qk.entity.Course;
import com.qk.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/courses")
@Slf4j
@RestController
public class CourseController {

    @Autowired
    private CourseService courseService;
    @GetMapping
    public Result list(CourseDto courseDto) {
        log.info("查询到的课表有：" + courseDto);
        PageResult pageResult =courseService.list(courseDto);
        return Result.success(pageResult);
    }

    @Log
    @PostMapping
    public Result add(@RequestBody Course course) {
        log.info("添加课程" + course);
        courseService.add(course);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id){
        log.info("查询id:{}",id);
        Course course =courseService.findById(id);
        return Result.success(course);
    }
    @Log
    @PutMapping
    public Result update(@RequestBody Course course) {
        log.info("修改课程" + course);
        courseService.update(course);
        return Result.success();
    }

    @Log
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("删除id:{}",id);
        courseService.deleteById(id);
        return Result.success();
    }

    @GetMapping("/subject/{subject}")
    public Result findSubject(@PathVariable Integer subject){
        log.info("查询课程" + subject);
        List<Course> course = courseService.findSubject(subject);
        return Result.success(course);
    }

    @GetMapping("/list")
    public Result allSubject(){
        log.info("查询所有课程");
        List<Course> course = courseService.allSubject();
        return Result.success(course);
    }
}
